<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;

/**
 * Freewind主题专属音乐插件
 *
 * @package Freewind Music
 * @author Mr丶冷文
 * @version 1.0.0
 * @link https://kevinlu98.cn/
 */
class FreewindMusic_Plugin implements Typecho_Plugin_Interface
{
    /**
     * 激活插件方法,如果激活失败,直接抛出异常
     *
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function activate()
    {
        Typecho_Plugin::factory('freewind')->rightToolBar = array('FreewindMusic_Plugin', 'render');
        Typecho_Plugin::factory('freewind')->css = array('FreewindMusic_Plugin', 'css');
        Typecho_Plugin::factory('freewind')->js = array('FreewindMusic_Plugin', 'js');
    }

    /**
     * 禁用插件方法,如果禁用失败,直接抛出异常
     *
     * @static
     * @access public
     * @return void
     * @throws Typecho_Plugin_Exception
     */
    public static function deactivate()
    {
    }

    /**
     * 获取插件配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form 配置面板
     * @return void
     */
    public static function config(Typecho_Widget_Helper_Form $form)
    {
        $name = new Typecho_Widget_Helper_Form_Element_Text('name', NULL, '', _t('歌曲名称'));
        $form->addInput($name);
        $music = new Typecho_Widget_Helper_Form_Element_Text('music', NULL, '', _t('音频文件地址'));
        $form->addInput($music);
        $songer = new Typecho_Widget_Helper_Form_Element_Text('songer', NULL, '', _t('歌手'));
        $form->addInput($songer);
        $avatar = new Typecho_Widget_Helper_Form_Element_Text('avatar', NULL, '', _t('歌手图片'));
        $form->addInput($avatar);
        $word = new Typecho_Widget_Helper_Form_Element_Textarea('word', NULL, '', _t('LRC歌词'), "一定要用有效歌词，不然播放器识别不了会报错");
        $form->addInput($word);
    }

    /**
     * 个人用户的配置面板
     *
     * @access public
     * @param Typecho_Widget_Helper_Form $form
     * @return void
     */
    public static function personalConfig(Typecho_Widget_Helper_Form $form)
    {
    }

    /**
     * 插件实现方法
     *
     * @access public
     * @return void
     */
    public static function render()
    {
        ?>
        <div id="right-music" class="right-tool-item" style="width: 300px;">
            <a class="right-btn" data-target="right-music" href="javascript:void(0)">
                <i style="font-size: 20px" class="iconfont icon-yinlegedanyinfu"></i></a>
            <div class="right-title">MUSIC</div>
            <div class="right-content" style="border-radius: 0 0 10px 10px">
                <div id="music-player" class="aplayer">
                <pre class="aplayer-lrc-content">
                    <?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindMusic')->word ?>
                </pre>
                </div>
            </div>
        </div>
        <?php
    }

    public static function css()
    {
        echo '<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kevinlu98/freecdn@1.14/plugin/aplayer/APlayer.min.css">';
    }

    public static function js()
    {
        echo '<script src="https://cdn.jsdelivr.net/gh/kevinlu98/freecdn@1.14/plugin/aplayer/APlayer.min.js"></script>';
        ?>
        <script>
            var ap3 = new APlayer({
                element: document.getElementById('music-player'),//样式1
                narrow: false,
                autoplay: false,
                showlrc: true,
                music: {
                    title: '<?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindMusic')->name ?>',
                    author: '<?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindMusic')->songer ?>',
                    url: '<?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindMusic')->music ?>',
                    pic: '<?php echo Typecho_Widget::widget('Widget_Options')->plugin('FreewindMusic')->avatar ?>'
                }
            });
            ap3.init();
        </script>
        <?php

    }
}
